package lesson.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lesson.entity.Academy;
import lesson.service.AcademyService;
import lesson.vo.AcademyVo;
import lesson.vo.DataView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description:
 * @Author: Wanzhen
 * @Date: 2022/6/13-22:48
 */
@Controller
@RequestMapping("/academy")
public class AcademyController {

    @Autowired
    public AcademyService academyService;

    @RequestMapping("/toAcademy")
    public String toAcademy(){
        return "academy/academy";
    }

    @RequestMapping("/listAcademy")
    @ResponseBody
    public DataView listAcademy(AcademyVo academyVo){
        QueryWrapper<Academy> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(academyVo.getName()),"name",academyVo.getName());
        IPage<Academy> iPage = new Page<>(academyVo.getPage(), academyVo.getLimit());
        academyService.page(iPage, queryWrapper);
        return new DataView(iPage.getTotal(), iPage.getRecords());
    }

    /**
     * 新增或修改
     * @return
     */
    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public DataView addOrUpdate(Academy academy){
        academyService.saveOrUpdate(academy);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("新增或修改学院成功");
        return dataView;
    }

    @RequestMapping("/deleteById")
    public DataView deleteById(Integer id){
        academyService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除学院成功");
        return dataView;
    }

}
